package com.suqiu.config.rabbitmq;

import com.suqiu.config.rabbitmq.exchange.RabbitExchangeConst;
import com.suqiu.config.rabbitmq.queue.RabbitQueueConst;
import com.suqiu.config.rabbitmq.routingkey.RabbitRoutingKeyConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: suqiu
 * @Date: 2022-02-10 10:47
 **/
@Configuration
public class RabbitmqConfig {

    //声明交换机
    @Bean(RabbitExchangeConst.EXCHANGE_NAME)
    public Exchange exchange() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(RabbitExchangeConst.EXCHANGE_NAME).durable(true).build();
    }


    /**
     * new Queue(QUEUE_EMAIL,true,false,false)
     * durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
     * auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
     * exclusive  表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean(RabbitQueueConst.QUEUE_EMAIL)
    public Queue emailQueue() {
        return new Queue(RabbitQueueConst.QUEUE_EMAIL);
    }

    @Bean(RabbitQueueConst.QUEUE_SMS)
    public Queue smsQueue() {
        return new Queue(RabbitQueueConst.QUEUE_SMS);
    }

    //ROUTINGKEY_EMAIL队列绑定交换机，指定routingKey
    @Bean
    public Binding bindingEmail(@Qualifier(RabbitQueueConst.QUEUE_EMAIL) Queue queue,
                                @Qualifier(RabbitExchangeConst.EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitRoutingKeyConst.ROUTINGKEY_EMAIL).noargs();
    }

    //ROUTINGKEY_SMS队列绑定交换机，指定routingKey
    @Bean
    public Binding bindingSMS(@Qualifier(RabbitQueueConst.QUEUE_SMS) Queue queue,
                              @Qualifier(RabbitExchangeConst.EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitRoutingKeyConst.ROUTINGKEY_SMS).noargs();
    }

}
